package cate.game.pvp.throne;

import cate.common.table.activity.tbboss.TBChampionBossRow;
import cate.common.table.d.GDOperation;
import cate.common.util.TimeTool;
import cate.game.play.param.HpExtend;
import cate.game.play.proce.play.PlayPO;
import cate.game.play.vo.ReplayUtils;
import cate.game.pvp.throne.data.boss.TBChampionBossReplay;
import cate.game.pvp.throne.msg.TBBossResultInnerReq;
import cate.game.pvp.throne.msg.TBBossResultInnerResp;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.RoleSimpleBase;
import cate.game.role.farm.AbstractFarmFTC;
import cate.game.role.pvp.throne.RoleTBChampionBoss;
import cate.game.role.pvp.throne.msg.TBChampionBossChallengeResp;
import cp.solution.manage.api.open.replay.ReplaySaveReq;

import java.util.Set;
import java.util.stream.Collectors;

public class TBChampionBossFTC extends AbstractFarmFTC<RoleTBChampionBoss> {

	private boolean sim;

	private TBChampionBossRow bossRow;

	private HpExtend hpExtend;

	@Override
	protected RoleTBChampionBoss po(Role role) {
		return role.getPvp().throne.championBoss;
	}

	public TBChampionBossFTC(Role role, boolean sim, TBChampionBossRow bossRow,HpExtend hpExtend) {
		setup(role);
		this.sim = sim;
		this.bossRow = bossRow;
		this.hpExtend = hpExtend;
	}


	@Override
	public void handleResult(Role role, PlayPO playPO) {
		super.handleResult(role, playPO);
		TBChampionBossChallengeResp resp = new TBChampionBossChallengeResp();
		resp.win = true;
		if (!sim) {
			hpExtend.recordByTid(playPO.teamEnd.b);
			boolean kill = hpExtend.getTidRates().getOrDefault(bossRow.bossTid, 0d) <= 0;
			Set<String> heroUids = playPO.a.hs.stream().map(h -> h.hero.uid).collect(Collectors.toSet());

			TBChampionBossReplay replay = new TBChampionBossReplay();
			replay.base = new RoleSimpleBase().read(role.getBase());
			try {
				ReplaySaveReq saveReq = ReplayUtils.toReplaySaveReq(playPO, role.getGame().time.now() + TimeTool.TimeCons.DAY * 3);
				replay.replayUid = role.getGame().openApi().saveReplay(saveReq).data.getId();
			} catch (Exception e) {
				e.printStackTrace();
			}
			//跨服同步
			TBBossResultInnerResp reportResp = role.getGame().pvp.throne.message.bossReport(TBBossResultInnerReq.of(kill, hpExtend, heroUids, replay));
			if (reportResp.result.ok() && kill) {
				MixRes reward = new MixRes(bossRow.rwdStr);
				new MixResAdder().setRes(reward).setOperation(GDOperation.THRONE_CHAMPION_BOSS).setDialogReward(true).exe(role);
				resp.reward = reward;
			}
		}
		role.sendNow(resp);
	}

}
